####Info####
# This is a function created by Ruoyan.
# The problem that this function want to solve is to draw a line in scatter plot
# in fact, I will add lots of points on the graph

points_line <- function(x_interval = c(0, 1), 
                        formula = "x", precise = 100, 
                        type_code = "line",
                        inverse_yn = F,
                        typename = "type",
                        x_name = "x",
                        y_name = "y"
                        ){
  # generate x value
  unit <- (max(x_interval) - min(x_interval))/precise
  x <- rep(0, precise)
  for(i in 1:precise){
    x[i] <- min(x_interval) + unit * i
  }
  
  # generate y value
  
  order <- paste("y <- ", formula, sep = "")
  
  eval(parse(text = order))
  
  # generate type
  
  type <- rep(type_code, precise)
  
  if(inverse_yn){
    return_df <- data.frame(y, x, type)
    names(return_df) <- c(x_name, y_name, typename)
    return(return_df)
  }
  return_df <- data.frame(x, y, type)
  names(return_df) <- c(x_name, y_name, typename)
  return(return_df)
  
}


line_to_plot_formula <- function(vec = c(0, 0, 0)){
  # first num
  return(paste("-(",vec[1]/vec[2], ") * x - (",vec[3]/vec[2], ")", sep = ""))
}

# line_to_plot_formula(vec = c(5,-5,3))
# ggplot(points_line(x_interval = c(-1, 2),
#                    formula = line_to_plot_formula(c(-2,1,-1)),
#                    precise = 1000,
#                    type_code = "c",
#                    typename = "color",
#                    x_name = "fakedatax",
#                    y_name = "fakedatay"
# ), aes(x = fakedatax, y = fakedatay, color = color)) + geom_point() + xlim(-1,6) + ylim(-1,6)

# library(ggplot2)
# 
# ggplot(points_line(x_interval = c(-3, 12), formula = 
# "exp(1)^(-x^2) * cos(pi * (2/log(2, exp(1)))^0.5 * x) + 
# exp(1)^(-(x-1)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-1)) +
# exp(1)^(-(x-2)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-2)) +
# exp(1)^(-(x-3)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-3)) +
# exp(1)^(-(x-4)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-4)) +
# exp(1)^(-(x-5)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-5)) +
# exp(1)^(-(x-0.9)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-0.9)) +
# exp(1)^(-(x-0.3)^2) * cos(pi * (2/log(2, exp(1)))^0.5 * (x-0.3))", 
# precise = 6000),
#        aes(x = x, y = y, colour = type)) +
#   geom_point()
# 
# library(WaveletComp)
# 
# ?reconstruct
